
             
/**
 * 
 *
 * @File name:  QueryTFixedAssetsAction.java 
 * @Create on:  2011-10-31 17:00:07
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */
           
        
package com.boho.estates.check.action;

            
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.boho.framework.actions.ActionImp;
import cn.boho.framework.context.ActionContext;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.POUtils;
import cn.boho.framework.po.UpdatePOList;
import cn.boho.framework.service.MessageService;

import com.boho.base.dao.DaoHelper;
import com.boho.common.Constant;
import com.boho.common.DateUtils;
import com.boho.common.SystemUtils;
import com.boho.estates.check.dao.AdjustDao;
import com.boho.estates.po.TCheckAdjustPO;
import com.boho.estates.po.TCheckInfoDetailPO;
import com.boho.estates.po.TCheckInfoPO;
import com.boho.estates.po.TFaChangeFlowPO;

public class AdjustAssetAction extends ActionImp {
	
	private Connection con=null;
	private String checkNo;
	private Double chengBefullof;
	private Double chengLose;
	private Double befullofLose;
	private Object[] pyIds;
	private Object[] pkIds;
	@Override
	protected void doException(ActionContext atx, Exception ex) {
		atx.setErrorContext("BOHO_COMMON_1004", MessageService.getMessage("BOHO_COMMON_1004"),"【调账信息】" ,ex);
	}
	@Override
	protected int performExecute(ActionContext atx) throws Exception {
		
		Date date = new Date();
		
		//2.1修改盘点信息中的盘盈金额，盘点状态(2)，盘亏金额，盈亏差,盘点结束时间。
		TCheckInfoPO conditionCheckInfoPO = new TCheckInfoPO();
		TCheckInfoPO valueCheckInfoPO = new TCheckInfoPO();
		conditionCheckInfoPO.setCheckNo(checkNo);
		valueCheckInfoPO.setChengBefullof(chengBefullof); //盘盈金额
		valueCheckInfoPO.setChageStatus("2");
		valueCheckInfoPO.setChengLose(chengLose); //盘亏金额
		valueCheckInfoPO.setBefullofLose(befullofLose); //盈亏差
		valueCheckInfoPO.setChangeEndDate(date);
		POUtils.update(con, conditionCheckInfoPO, valueCheckInfoPO, false);
		
		//查询操作者信息
		DynaBeanMap persondbm = (DynaBeanMap)atx.getSessionValue(Constant.SESSION_PERSON_BEAN);
		String userId = SystemUtils.getSessionUser(atx);
		
		DaoHelper.setTDepartmentField(con, persondbm, "DEPARTMENT_NAME", "DEPARTMENT_NO");
		String name = (String)persondbm.get("NAME");
		String personNO = (String)persondbm.get("PERSONNAL_NO");
		String departmentName = (String)persondbm.get("DEPARTMENT_NAME");
		
		//2.2update 盘点明细 set 盈亏标识=盘亏，盘点时间=当前时间，盘点人=当前操作员，盘点部门=当前操作员所在部门 where 是否盘点=否
		TCheckInfoDetailPO conditionCheckInfoDetailPO = new TCheckInfoDetailPO();
		TCheckInfoDetailPO valueCheckInfoDetailPO = new TCheckInfoDetailPO();
		conditionCheckInfoDetailPO.setIsCheck(0);
		conditionCheckInfoDetailPO.setCheckNo(checkNo);
		valueCheckInfoDetailPO.setLoseStatus(0); //盘亏
		valueCheckInfoDetailPO.setCheckDate(date);
		valueCheckInfoDetailPO.setCheckPerson(name); //盘点人
		valueCheckInfoDetailPO.setCheckDemp(departmentName); //盘点人部门
		POUtils.update(con, conditionCheckInfoDetailPO, valueCheckInfoDetailPO, false);
		
		//2.3update 盘点明细 set 盈亏标识=平账 where 是否盘点=是
		TCheckInfoDetailPO conditionCheckInfoDetailPO1 = new TCheckInfoDetailPO();
		TCheckInfoDetailPO valueCheckInfoDetailPO1 = new TCheckInfoDetailPO();
		conditionCheckInfoDetailPO1.setIsCheck(1);
		conditionCheckInfoDetailPO1.setCheckNo(checkNo);
		valueCheckInfoDetailPO1.setLoseStatus(2); //平账
		POUtils.update(con, conditionCheckInfoDetailPO1, valueCheckInfoDetailPO1, false);
		
		//2.4insert into 盘点明细 values(是否盘点=是，盈亏标识=盘亏、盘盈。其他按记录写入);
		//盘盈
		if(pyIds.length != 0){
			StringBuffer idsSB = new StringBuffer();
			for (Object obj : pyIds) {
				idsSB.append((String)obj).append(",");
			}
			String ids = idsSB.substring(0, idsSB.length() - 1);
			List dbmList = AdjustDao.queryAssetsList(con, ids);
			DaoHelper.setTDepartmentField(con, dbmList, "DEPARTMENT_NAME", "GS_DEPARTMENT_NO");
			List poList = new ArrayList();
			for (Object obj : dbmList) {
				DynaBeanMap dbm = (DynaBeanMap)obj;
				TCheckInfoDetailPO po = new TCheckInfoDetailPO();
				po.setCheckNo(checkNo);
				po.setBarcode((String)dbm.get("BARCODE"));
				po.setFixedAssetsNo((Integer)dbm.get("ID"));
				po.setStockPrice((Double)dbm.get("STOCK_PRICE"));
				po.setFixedAssetsName((String)dbm.get("FIXED_ASSETS_NAME"));
				po.setDepartmentName((String)dbm.get("DEPARTMENT_NAME"));
				po.setStoredIn((String)dbm.get("STORED_IN"));
				po.setWithMananger((String)dbm.get("WITH_MANANGER"));
				po.setIsCheck(1);
				po.setLoseStatus(1);
				po.setCheckDate(date);
				po.setCheckPerson(name);
				po.setCheckDemp(departmentName);
				po.setCreateBy(userId);
				po.setCheckDate(date);
				poList.add(po);
			}
			POUtils.insertPoList(con, poList, false);
		}
		//盘亏
		if(pkIds.length != 0){
			UpdatePOList updatePOList = new UpdatePOList();
			for (Object obj : pkIds) {
				String id = (String)obj;
				TCheckInfoDetailPO conditionPKPO = new TCheckInfoDetailPO();
				TCheckInfoDetailPO valuePKPO = new TCheckInfoDetailPO();
				conditionPKPO.setId(Integer.valueOf(id));
				valuePKPO.setIsCheck(1);
				valuePKPO.setLoseStatus(0); //盘亏
				valuePKPO.setUpdateBy(userId);
				valuePKPO.setUpdateDate(date);
				updatePOList.add(conditionPKPO, valuePKPO);
			}
			POUtils.updatePoList(con, updatePOList, false);
		}	
		
		
		//2.5insert into 盘点调账信息 注意调账金额=盈亏金额
		TCheckAdjustPO checkAdjustPO = new TCheckAdjustPO();
		checkAdjustPO.setCheckNo(checkNo);
		checkAdjustPO.setAdjustDate(date);
		checkAdjustPO.setAdjustPerson(name);
		checkAdjustPO.setChageStatusadjustDemp(departmentName);
		checkAdjustPO.setChengLose(befullofLose); //盈亏金额
		checkAdjustPO.setCreateBy(userId);
		checkAdjustPO.setCreateDate(date);
		POUtils.insert(con, checkAdjustPO, false);
		
		//2.6update 资产详细信息 set 状态=盘亏出库 where 条形码 in 盘点明细中是否盘点状态为否的条形码记录。
		String sql = "update T_FIXED_ASSETS_DETAIL set FA_STATUS = '6' where BARCODE in (select BARCODE from T_CHECK_INFO_DETAIL where IS_CHECK = 0 and CHECK_NO = ?)";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, checkNo);
		ps.execute();
		ps.close();
		
		//2.7insert into 资产变更流水 将是否盘点状态为否的条形码进行变更，且变更类型为盘亏出库.原因：时间+盘亏出库，盘点单号为：XXX，盘点人为：XXXX。
		List dbm = AdjustDao.queryCheckInfoByCheckNo(con, checkNo);
		List sl = new ArrayList();
		DynaBeanMap checkdbm = null;
		StringBuffer sb = new StringBuffer();
		String pkdate = DateUtils.getDateTimeStrByFormat(date, "yyyy-MM-dd");
		for (Object object : dbm) {
			sb.append(pkdate).append("盘亏出库，盘点单号为：").append(checkNo).append("，盘点人为：").append(name);
			checkdbm = (DynaBeanMap)object;
			TFaChangeFlowPO taChangeFlowPO = new TFaChangeFlowPO();
			taChangeFlowPO.setBarcode((String)checkdbm.get("BARCODE"));
			taChangeFlowPO.setChangeType("6"); //出库
			taChangeFlowPO.setChangeDate(date);
			taChangeFlowPO.setChageReason(sb.toString());
			taChangeFlowPO.setBeforPrice((Double)checkdbm.get("STOCK_PRICE"));
			taChangeFlowPO.setAfterPrice((Double)checkdbm.get("STOCK_PRICE"));
			taChangeFlowPO.setPerson(personNO);
			taChangeFlowPO.setCreateBy(userId);
			taChangeFlowPO.setCreateDate(date);
			sl.add(taChangeFlowPO);
			sb.setLength(0);
		}
		POUtils.insertPoList(con, sl, false);
		
		return 1;
	}
	@Override
	protected int verifyParameters(ActionContext atx) throws Exception {
		checkNo = atx.getStringValue("CHECK_NO", "");
		chengBefullof = atx.getDoubleValue("CHENG_BEFULLOF", 0.0);
		chengLose = atx.getDoubleValue("CHENG_LOSE", 0.0);
		befullofLose = atx.getDoubleValue("BEFULLOF_LOSE", 0.0);
		pyIds = atx.getArrayValue("PY_IDS", new Object[0]);
		pkIds = atx.getArrayValue("PK_IDS", new Object[0]);
		con = atx.getConection();
		return 1;
	}
}
